/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sentry;

import io.sentry.util.Objects;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import org.jetbrains.annotations.NotNull;

/**
 * Resolves {@link RequestDetails}.
 *
 * @since 2021-06-02
 **/
final class RequestDetailsResolver {
    /**
     * HTTP Header for the user agent.
     */
    private static final String USER_AGENT = "User-Agent";
    /**
     * HTTP Header for the authentication to Sentry.
     */
    private static final String SENTRY_AUTH = "X-Sentry-Auth";

    private final @NotNull SentryOptions options;

    /**
     * constructor
     *
     * @param options options
     */
    RequestDetailsResolver(final @NotNull SentryOptions options) {
        this.options = Objects.requireNonNull(options, "options is required");
    }

    @NotNull
    RequestDetails resolve() {
        final Dsn dsn = new Dsn(options.getDsn());
        final URI sentryUri = dsn.getSentryUri();
        final String envelopeUrl = sentryUri.resolve(sentryUri.getPath() + "/envelope/").toString();

        final String publicKey = dsn.getPublicKey();
        final String secretKey = dsn.getSecretKey();

        final String authHeader =
            "Sentry sentry_version="
                + SentryClient.SENTRY_PROTOCOL_VERSION
                + ","
                + "sentry_client="
                + options.getSentryClientName()
                + ","
                + "sentry_key="
                + publicKey
                + (secretKey != null && secretKey.length() > 0 ? (",sentry_secret=" + secretKey) : "");
        final String userAgent = options.getSentryClientName();

        final Map<String, String> headers = new HashMap<>();
        headers.put(USER_AGENT, userAgent);
        headers.put(SENTRY_AUTH, authHeader);

        return new RequestDetails(envelopeUrl, headers);
    }
}
